<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="区域" prop="areaid">
        <el-select v-model="queryParams.areaid" placeholder="请选择区域" clearable>
          <el-option
            v-if="item.ancestors.length === 9"
            v-for="item in deptList"
            :key="item.value"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检测点地址" prop="caddress" label-width="125px">
        <el-input
          v-model="queryParams.caddress"
          placeholder="请输入检测点地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测点名称" prop="cpname" label-width="125px">
        <el-input
          v-model="queryParams.cpname"
          placeholder="请输入检测点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测点状态" prop="cpstatus" label-width="125px">
        <el-select v-model="queryParams.cpstatus" placeholder="请选择检测点状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

<!--      <el-form-item label="电话" prop="phone">-->
<!--        <el-input-->
<!--          v-model="queryParams.phone"-->
<!--          placeholder="请输入电话"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="经度" prop="longitude">-->
<!--        <el-input-->
<!--          v-model="queryParams.longitude"-->
<!--          placeholder="请输入经度"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="维度" prop="latitude">-->
<!--        <el-input-->
<!--          v-model="queryParams.latitude"-->
<!--          placeholder="请输入维度"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最大检测人数" prop="maxuser" label-width="150px">-->
<!--        <el-input-->
<!--          v-model="queryParams.maxuser"-->
<!--          placeholder="请输入最大检测人数"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最大医护人员" prop="maxblackuser" label-width="150px">-->
<!--        <el-input-->
<!--          v-model="queryParams.maxblackuser"-->
<!--          placeholder="请输入最大医护人员"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['whc:checkpoint:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['whc:checkpoint:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['whc:checkpoint:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['whc:checkpoint:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkpointList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="检测点id" align="center" prop="cpid" />
      <el-table-column label="区域" align="center" prop="dept.deptName" />
      <el-table-column label="检测点地址" align="center" prop="caddress" />
      <el-table-column label="检测点名称" align="center" prop="cpname" />
      <el-table-column label="检测点状态" align="center" prop="cpstatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.cpstatus"/>
        </template>
      </el-table-column>
      <el-table-column label="开启时间" align="center" prop="startdate" />
      <el-table-column label="结束时间" align="center" prop="endtime" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="维度" align="center" prop="latitude" />
      <el-table-column label="最大检测人数" align="center" prop="maxuser" />
      <el-table-column label="最大医护人员" align="center" prop="maxblackuser" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['whc:checkpoint:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['whc:checkpoint:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检测点管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-form-item label="区域" prop="areaid">
<!--          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />-->
          <el-select v-model="form.areaid" placeholder="请选择区域">
            <el-option v-if="item.ancestors.length === 9"  v-for="item in deptList" :label="item.deptName" :value="item.deptId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检测点地址" prop="caddress">
          <el-input v-model="form.caddress" placeholder="请输入检测点地址" />
        </el-form-item>
        <el-form-item label="检测点名称" prop="cpname">
          <el-input v-model="form.cpname" placeholder="请输入检测点名称" />
        </el-form-item>
        <el-form-item label="检测点状态">
          <el-radio-group v-model="form.cpstatus">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-table-column label="开启时间" align="center" prop="startdate" />
        <el-table-column label="结束时间" align="center" prop="endtime" />
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="维度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="最大检测人数" prop="maxuser">
          <el-input v-model="form.maxuser" placeholder="请输入最大检测人数" />
        </el-form-item>
        <el-form-item label="最大医护人员" prop="maxblackuser">
          <el-input v-model="form.maxblackuser" placeholder="请输入最大医护人员" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckpoint, getCheckpoint, delCheckpoint, addCheckpoint, updateCheckpoint } from "@/api/whc/checkpoint";
import {listDept} from "../../../api/system/dept";

export default {
  name: "Checkpoint",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 表格树数据
      deptList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测点管理表格数据
      checkpointList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 最大医护人员时间范围
      daterangeStartdate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaid: null,
        caddress: null,
        cpname: null,
        cpstatus: null,
        startdate: null,
        phone: null,
        longitude: null,
        latitude: null,
        maxuser: null,
        maxblackuser: null
      },
      nullParams: {
        deptName: undefined,
        status: undefined
      },
      // 部门树选项
      // deptOptions: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    // this.getDeptTree();
    this.getListDept();
  },
  methods: {
    /** 查询区域列表 */
    getListDept() {
      this.loading = true;
      listDept(this.nullParams).then(response => {
        this.deptList = response.data
        console.log(this.deptList)
        this.loading = false;
      });
    },
    // getDeptTree() {
    //   deptTreeSelect().then(response => {
    //     this.deptOptions = response.data;
    //     console.log(response.data)
    //   });
    // },
    /** 查询检测点管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeStartdate && '' != this.daterangeStartdate) {
        this.queryParams.params["beginStartdate"] = this.daterangeStartdate[0];
        this.queryParams.params["endStartdate"] = this.daterangeStartdate[1];
      }
      listCheckpoint(this.queryParams).then(response => {
        this.checkpointList = response.rows;
        console.log(response.rows)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cpid: null,
        areaid: null,
        caddress: null,
        cpname: null,
        startdate: null,
        endtime:null,
        cpstatus: "0",
        phone: null,
        longitude: null,
        latitude: null,
        maxuser: null,
        maxblackuser: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeStartdate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cpid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检测点管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cpid = row.cpid || this.ids
      getCheckpoint(cpid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检测点管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cpid != null) {
            updateCheckpoint(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckpoint(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cpids = row.cpid || this.ids;
      this.$modal.confirm('是否确认删除检测点管理编号为"' + cpids + '"的数据项？').then(function() {
        return delCheckpoint(cpids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('whc/checkpoint/export', {
        ...this.queryParams
      }, `checkpoint_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
